package com.sinosoft.perfect.system._.util;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

public class XmlUtil {

	public static Document createDocument() {
		return DocumentHelper.createDocument();
	}

	public static Element createElement(String elementName) {
		return DocumentHelper.createElement(elementName);
	}

	public static void addElement(Document document, Element element) {
		document.add(element);
	}

	public static void addElementChild(Element elementParent, Element elementChild) {
		elementParent.add(elementChild);
	}

	public static Element createRootElement(Document document, String rootElementName) {
		return document.addElement(rootElementName);
	}

	public static void editElementAttribute(Element element, String attrName, String attrValue) {
		element.addAttribute(attrName, attrValue);
	}

	public static void editElementAttribute(Element element, Map<String, Object> attributeMap) {
		Set<Entry<String, Object>> set = attributeMap.entrySet();
		Iterator<Entry<String, Object>> it = set.iterator();
		while (it.hasNext()) {
			Entry<String, Object> entry = it.next();
			element.addAttribute(entry.getKey(), (String) entry.getValue());
		}

	}

	public static String getDocumentToXmlString(Document document) {
		return document.asXML();
	}

	public static void editElementText(Element element, String text) {
		element.addText(text);
	}
}
